package com.yswl.pri.picture.adapter;

import android.Manifest;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.request.RequestOptions;
import com.yswl.pri.R;
import com.yswl.pri.picture.entry.ImageBean;
import com.yswl.pri.picture.net.PictrueSaveAsyTask;
import com.yswl.pri.picture.widgets.BottomDialog;

import java.lang.ref.WeakReference;
import java.util.List;

import yswl.com.klibrary.base.MActivity;
import yswl.com.klibrary.permision.PermissionListener;
import yswl.com.klibrary.permision.PermissionUtil;
import yswl.com.klibrary.util.L;
import yswl.com.klibrary.util.MKeyBoardUtils;
import yswl.com.klibrary.util.ToastUtil;

public  class PictureDialogFragmentPagerAdapter extends PagerAdapter {
    private static final String TAG = PictureDialogFragmentPagerAdapter.class.getSimpleName();
    List<View> views;
    List<ImageBean> images;
    WeakReference<FragmentActivity> mWeakActivity;
    public PictureDialogFragmentPagerAdapter(FragmentActivity activity) {
        this.mWeakActivity = new WeakReference<>(activity);
    }

    @Override
    public int getCount() {
        return views == null ? 0 : views.size();
    }

    public void setViews(List<ImageBean> images,List<View> views) {
        this.images = images;
        this.views = views;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(views.get(position));
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = views.get(position);
        final ImageBean imageBean = images.get(position);
        final ImageView imageView = view.findViewById(R.id.photoview);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                PictureDialog.this.dismiss();
            }
        });
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final BottomDialog dialog = new BottomDialog(v.getContext());
                dialog.setOnCopy(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        MKeyBoardUtils.copy(imageBean.image_url, mWeakActivity.get());
                        ToastUtil.showToast("复制成功");
                    }
                });
                dialog.setOnSave(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        L.e(TAG, "  threadId = " + Thread.currentThread().getId());
                        dialog.dismiss();
                        PermissionUtil permiss = new PermissionUtil(mWeakActivity.get());
                        permiss.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionListener() {
                            @Override
                            public void onGranted() {
                                savaPicture(imageBean.image_url);
                            }

                            @Override
                            public void onDenied(List<String> deniedPermission) {
                                ToastUtil.showToast("图片保存失败，请同意内存卡读取权限");
                            }

                            @Override
                            public void onDeniedForever(List<String> deniedPermission) {
                                ToastUtil.showToast("请在设置面板，打开权限");
                            }
                        });
                    }
                });
                if (!dialog.isShowing()) {
                    dialog.show();
                }
                return true;
            }
        });

        if (imageBean.image_width == 0 || imageBean.image_height == 0) {
            Glide.with(view.getContext()).asBitmap()
                    .apply(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888))
                    .load(imageBean.image_url).thumbnail(0.01f).into(imageView);
        } else {
            Glide.with(view.getContext()).applyDefaultRequestOptions(new RequestOptions()
                    .override(imageBean.image_width, imageBean.image_height).format(DecodeFormat.DEFAULT))
                    .load(imageBean.image_url).thumbnail(0.01f).into(imageView);
        }

        container.addView(views.get(position));
        return view;
    }


    private void savaPicture(final String imgurl) {
        new PictrueSaveAsyTask(mWeakActivity.get()).execute(imgurl);

    }


}